package com.cpi.cornpp.futurepurchase.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.futurepurchase.exception.FuturePurchaseException;
import com.cpi.cornpp.futurepurchase.form.SearchFuturePurchaseForm;
import com.cpi.cornpp.futurepurchase.services.FuturePurchaseService;
import com.cpi.cornpp.period.service.PeriodService;
import com.cs.common.init.SystemParameterCollection;
import com.cs.common.init.beans.SystemParameterBean;

public class ShowCustomerFuturePurchaseQueryAction extends Action {
	private static final String ERROR_MESSAGE = "ERROR_MESSAGE";
	private static final String SUCCESS_FORWARD = "toSuccess";
	private static final String FAILURE_FORWARD = "error";
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws FuturePurchaseException {
		int orderField = 0;
		int orderType = 0;
		int currentYear = 0;
		int topForwardYears = 0;
		String messageError = null;
		SystemParameterBean systemParameterBean = null;
		SearchFuturePurchaseForm sfpForm = null;
		FuturePurchaseService fupService = null;
		PeriodService periodService = null;
		List customerFuturePurchasesView = null;
		List periods = null;
		List periodYears = null;
		HttpSession httpSession = null;
		ActionForward forward = null;
		MessageResources messageResources = null;
		
		forward = mapping.findForward(FAILURE_FORWARD);
		httpSession = request.getSession(false);
		messageResources = getResources(request);
		
		try{
			
			if (form != null){
				periodYears = new ArrayList(0);
				fupService = new FuturePurchaseService();
				periodService = new PeriodService();
				sfpForm = (SearchFuturePurchaseForm) form;	
				
				orderField = sfpForm.getOrderField();
				orderType = sfpForm.getOrderType();
				periods = periodService.getActivePeriods();		
				currentYear = Calendar.getInstance().get(Calendar.YEAR);
				systemParameterBean = SystemParameterCollection.getInstance().getSystemParameterBean("TOP_FORWARD_YEARS");
				topForwardYears = systemParameterBean.getIntValue();

				customerFuturePurchasesView = fupService.getCustomerFuturePurchasesByCriteria(StringUtils.EMPTY, StringUtils.EMPTY, currentYear, currentYear + 1, orderField, (orderType == 1) ? true : false);
				
				for (int x = 0; x < (topForwardYears * 2); x++){
					periodYears.add((x < topForwardYears) ? Integer.toString((currentYear - topForwardYears + x)) : Integer.toString((currentYear - topForwardYears + x)));
				}
				
				request.setAttribute("periods", periods);
				request.setAttribute("periodYears", periodYears);
				request.setAttribute("bPeriodYear", new Integer( currentYear ));
				request.setAttribute("ePeriodYear", new Integer( currentYear+1));
				request.setAttribute("customerFuturePurchases", customerFuturePurchasesView);
				
				forward = mapping.findForward(SUCCESS_FORWARD);
			}
			
		} catch(FuturePurchaseException futurePurchaseException){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), futurePurchaseException.getErrorCode());
			if(null == messageError){
				form.reset(mapping, request);
				throw futurePurchaseException;
			}else{
				httpSession = request.getSession(false);
				httpSession.setAttribute(ERROR_MESSAGE, messageError);
				forward = mapping.findForward(FAILURE_FORWARD);
			}
		}catch(Exception exception){
			FuturePurchaseException futurePurchaseException = null;
			exception.printStackTrace(System.out);
			futurePurchaseException = new FuturePurchaseException(exception , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			form.reset(mapping, request);
			throw futurePurchaseException;
		}
		
		httpSession.setAttribute("previousOrderField", Integer.toString(orderField));
		httpSession.setAttribute("previousOrderType", Integer.toString(orderType));

		
		return forward;
	}
}